$(function () {
    getArticleData();

    function getArticleData(key = '', type = '', state = '', page = 1, perpage = 10) {
        const url = '/admin/article/query';
        ajax('get', url, {
            key,
            type,
            state,
            page,
            perpage
        }).then(res => {
            console.log(res.data.data)
            const {
                data
            } = res.data;
            const ele = template('articleLists', {
                data
            });
            // console.log(ele);
            $('table tbody').html(ele);
        })
    }

    // 事件绑定,编辑文章
    $('table tbody').on('click', '.editBtn', function (e) {
        e.preventDefault();
        const id = $(this).data('id');
        // 跳转
        location.href = 'article_edit.html?id=' + id;
    })

    //删除
    $('table tbody').on('click', '.delBtn', function (e) {
        if (confirm("您确定要删除此篇文章?")) {
            const id = $(this).data('id');
            // 发请求删除指定的数据
            const url = '/admin/article/delete';
            ajax('post', url, {
                id
            }).then(res => {
                // 渲染页面
                getArticleData();
            })
        }
    })

    // 帅选功能
    $('#btnSearch').click(function () {
        // 获取输入框的值
        const key = $.trim($('#aname').val());
        // 获取所有分类
        const type = $('#selCategory').val();
        const state = $('#selStatus').val();

        //  筛选请求
        getArticleData(key, type, state);
    })
})